package design;

//访问者模式，对于不同的访问者，访问的形式不一样，如CTO关注工程师的代码量、经理的新产品数量;CEO 关注工程师的KPI、经理的KPI及新产品数量。

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

interface IVisitor{
    //访问工程师类型
    void visit(Engineer engineer);

    //访问经理类型
    void visit(Manager manager);
}


abstract class Employee{
    public String name;
    public int kpi;

    public Employee(String name) {
        this.name = name;
        this.kpi = new Random().nextInt(10);
    }

    public abstract void accept(IVisitor visitor);
}

class Engineer extends Employee{

    public Engineer(String name) {
        super(name);
    }

    public int getCodeLines(){
        return new Random().nextInt(10*10000);
    }

    @Override
    public void accept(IVisitor visitor) {
        visitor.visit(this);
    }
}

class Manager extends Employee{

    public Manager(String name) {
        super(name);
    }

    public int getProducts(){
        return new Random().nextInt(10);
    }

    public void accept(IVisitor visitor) {
        visitor.visit(this);
    }
}

class BusinessReport{
    private List<Employee> employees = new LinkedList<Employee>();

    public BusinessReport(){
        employees.add(new Manager("经理-A"));
        employees.add(new Engineer("工程师-A"));

    }

    public void showReport(IVisitor visitor){
        for(Employee employee : employees){
            employee.accept(visitor);
        }
    }
}

class CEOVisitor implements IVisitor{

    public void visit(Engineer engineer) {
        System.out.println("工程师:" + engineer.name + ",KPI:" + engineer.kpi);
    }

    public void visit(Manager manager) {
        System.out.println("经理:" + manager.name + ",KPI:" + manager.kpi + ",新产品数量:" + manager.getProducts());
    }
}

class CTOVisitor implements IVisitor{

    public void visit(Engineer engineer) {
        System.out.println("工程师:" + engineer.name + ",代码行数:" + engineer.getCodeLines());
    }

    public void visit(Manager manager) {
        System.out.println("经理:" + manager.name + ",新产品数量:" + manager.getProducts());
    }
}

public class Client
{
    public static void main(String[] args) {
        BusinessReport report = new BusinessReport();
        System.out.println("========CEO看报表=============");
        report.showReport(new CEOVisitor());
        System.out.println("========CTO看报表=============");
        report.showReport(new CTOVisitor());
    }


}
